package cn.ibizlab.odoo.core.odoo_stock.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Istock_location;
import cn.ibizlab.odoo.core.odoo_stock.domain.Stock_location;
import cn.ibizlab.odoo.core.odoo_stock.filter.Stock_locationSearchContext;
import cn.ibizlab.odoo.core.odoo_stock.service.IStock_locationService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[库存位置] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Stock_locationServiceImpl implements IStock_locationService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Istock_locationClientService stock_locationClientService;

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Istock_location clientModel = stock_locationClientService.createModel();
        clientModel.setId(id);
        stock_locationClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //stock_locationClientService.removeBatch(stock_locations) ;

    }

    @Override
    public void updateBatch(List<Stock_location> stock_locations, int batchSize) {

        //stock_locationClientService.updateBatch(stock_locations,batchSize) ;

    }

    @Override
    public boolean create(Stock_location stock_location) {
        boolean bOk = false ;
        Istock_location clientModel = this.convert2Model(stock_location,null);
        stock_locationClientService.create(clientModel) ;
        this.convert2Domain(clientModel,stock_location);
        bOk = true ;

        return bOk ;
    }

    @Override
    public Stock_location get(Integer id) {
        Istock_location clientModel = stock_locationClientService.createModel();
        clientModel.setId(id);
        stock_locationClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Stock_location get(Stock_location stock_location) {
        Stock_location retstock_location = this.get(stock_location.getId()) ;
    	BeanCopier copier=BeanCopier.create(retstock_location.getClass(), stock_location.getClass(), false);
        copier.copy(retstock_location, stock_location, null);
        return stock_location ;
    }

    @Override
    public boolean update(Stock_location stock_location) {
        boolean bOk = false ;
        Istock_location clientModel = this.convert2Model(stock_location,null);
        stock_locationClientService.update(clientModel) ;
        this.convert2Domain(clientModel,stock_location) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Stock_location> stock_locations, int batchSize) {

                        //stock_locationClientService.createBatch(stock_locations,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Stock_location> searchDefault(Stock_locationSearchContext context) {
        List<Stock_location> list = new ArrayList<Stock_location>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_location> clientModelList = stock_locationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_location clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_location>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据科目查询
     */
    @Override
    public Page<Stock_location> selectByOdoo_valuation_in_account(Integer id){
        Stock_locationSearchContext context = new Stock_locationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_location> list = new ArrayList<Stock_location>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_location> clientModelList = stock_locationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_location clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_location>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据科目查询
     */
    @Override
    public Page<Stock_location> selectByOdoo_valuation_out_account(Integer id){
        Stock_locationSearchContext context = new Stock_locationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_location> list = new ArrayList<Stock_location>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_location> clientModelList = stock_locationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_location clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_location>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据上架策略查询
     */
    @Override
    public Page<Stock_location> selectByOdoo_putaway_strategy(Integer id){
        Stock_locationSearchContext context = new Stock_locationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_location> list = new ArrayList<Stock_location>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_location> clientModelList = stock_locationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_location clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_location>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据下架策略查询
     */
    @Override
    public Page<Stock_location> selectByOdoo_removal_strategy(Integer id){
        Stock_locationSearchContext context = new Stock_locationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_location> list = new ArrayList<Stock_location>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_location> clientModelList = stock_locationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_location clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_location>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据公司查询
     */
    @Override
    public Page<Stock_location> selectByOdoo_company(Integer id){
        Stock_locationSearchContext context = new Stock_locationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_location> list = new ArrayList<Stock_location>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_location> clientModelList = stock_locationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_location clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_location>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Stock_location> selectByOdoo_partner(Integer id){
        Stock_locationSearchContext context = new Stock_locationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_location> list = new ArrayList<Stock_location>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_location> clientModelList = stock_locationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_location clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_location>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Stock_location> selectByOdoo_create(Integer id){
        Stock_locationSearchContext context = new Stock_locationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_location> list = new ArrayList<Stock_location>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_location> clientModelList = stock_locationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_location clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_location>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Stock_location> selectByOdoo_write(Integer id){
        Stock_locationSearchContext context = new Stock_locationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_location> list = new ArrayList<Stock_location>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_location> clientModelList = stock_locationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_location clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_location>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据库存位置查询
     */
    @Override
    public Page<Stock_location> selectByOdoo_location(Integer id){
        Stock_locationSearchContext context = new Stock_locationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_location> list = new ArrayList<Stock_location>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_location> clientModelList = stock_locationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_location clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_location>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Istock_location convert2Model(Stock_location domain , Istock_location model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = stock_locationClientService.createModel() ;
        }

         if(domain.getPosxDirtyFlag())
            model.setPosx(domain.getPosx());
         if(domain.getPosyDirtyFlag())
            model.setPosy(domain.getPosy());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getBarcodeDirtyFlag())
            model.setBarcode(domain.getBarcode());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getPoszDirtyFlag())
            model.setPosz(domain.getPosz());
         if(domain.getComplete_nameDirtyFlag())
            model.setComplete_name(domain.getComplete_name());
         if(domain.getScrap_locationDirtyFlag())
            model.setScrap_location(domain.getScrap_location());
         if(domain.getReturn_locationDirtyFlag())
            model.setReturn_location(domain.getReturn_location());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getParent_pathDirtyFlag())
            model.setParent_path(domain.getParent_path());
         if(domain.getQuant_idsDirtyFlag())
            model.setQuant_ids(domain.getQuant_ids());
         if(domain.getUsageDirtyFlag())
            model.setUsage(domain.getUsage());
         if(domain.getCommentDirtyFlag())
            model.setComment(domain.getComment());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getChild_idsDirtyFlag())
            model.setChild_ids(domain.getChild_ids());
         if(domain.getLocation_id_textDirtyFlag())
            model.setLocation_id_text(domain.getLocation_id_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getValuation_out_account_id_textDirtyFlag())
            model.setValuation_out_account_id_text(domain.getValuation_out_account_id_text());
         if(domain.getPartner_id_textDirtyFlag())
            model.setPartner_id_text(domain.getPartner_id_text());
         if(domain.getRemoval_strategy_id_textDirtyFlag())
            model.setRemoval_strategy_id_text(domain.getRemoval_strategy_id_text());
         if(domain.getPutaway_strategy_id_textDirtyFlag())
            model.setPutaway_strategy_id_text(domain.getPutaway_strategy_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getValuation_in_account_id_textDirtyFlag())
            model.setValuation_in_account_id_text(domain.getValuation_in_account_id_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getValuation_in_account_idDirtyFlag())
            model.setValuation_in_account_id(domain.getValuation_in_account_id());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getLocation_idDirtyFlag())
            model.setLocation_id(domain.getLocation_id());
         if(domain.getValuation_out_account_idDirtyFlag())
            model.setValuation_out_account_id(domain.getValuation_out_account_id());
         if(domain.getPartner_idDirtyFlag())
            model.setPartner_id(domain.getPartner_id());
         if(domain.getPutaway_strategy_idDirtyFlag())
            model.setPutaway_strategy_id(domain.getPutaway_strategy_id());
         if(domain.getRemoval_strategy_idDirtyFlag())
            model.setRemoval_strategy_id(domain.getRemoval_strategy_id());
        return model ;
    }

    public Stock_location convert2Domain( Istock_location model ,Stock_location domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Stock_location();
        }

        if(model.getPosxDirtyFlag())
            domain.setPosx(model.getPosx());
        if(model.getPosyDirtyFlag())
            domain.setPosy(model.getPosy());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getBarcodeDirtyFlag())
            domain.setBarcode(model.getBarcode());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getPoszDirtyFlag())
            domain.setPosz(model.getPosz());
        if(model.getComplete_nameDirtyFlag())
            domain.setComplete_name(model.getComplete_name());
        if(model.getScrap_locationDirtyFlag())
            domain.setScrap_location(model.getScrap_location());
        if(model.getReturn_locationDirtyFlag())
            domain.setReturn_location(model.getReturn_location());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getParent_pathDirtyFlag())
            domain.setParent_path(model.getParent_path());
        if(model.getQuant_idsDirtyFlag())
            domain.setQuant_ids(model.getQuant_ids());
        if(model.getUsageDirtyFlag())
            domain.setUsage(model.getUsage());
        if(model.getCommentDirtyFlag())
            domain.setComment(model.getComment());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getChild_idsDirtyFlag())
            domain.setChild_ids(model.getChild_ids());
        if(model.getLocation_id_textDirtyFlag())
            domain.setLocation_id_text(model.getLocation_id_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getValuation_out_account_id_textDirtyFlag())
            domain.setValuation_out_account_id_text(model.getValuation_out_account_id_text());
        if(model.getPartner_id_textDirtyFlag())
            domain.setPartner_id_text(model.getPartner_id_text());
        if(model.getRemoval_strategy_id_textDirtyFlag())
            domain.setRemoval_strategy_id_text(model.getRemoval_strategy_id_text());
        if(model.getPutaway_strategy_id_textDirtyFlag())
            domain.setPutaway_strategy_id_text(model.getPutaway_strategy_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getValuation_in_account_id_textDirtyFlag())
            domain.setValuation_in_account_id_text(model.getValuation_in_account_id_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getValuation_in_account_idDirtyFlag())
            domain.setValuation_in_account_id(model.getValuation_in_account_id());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getLocation_idDirtyFlag())
            domain.setLocation_id(model.getLocation_id());
        if(model.getValuation_out_account_idDirtyFlag())
            domain.setValuation_out_account_id(model.getValuation_out_account_id());
        if(model.getPartner_idDirtyFlag())
            domain.setPartner_id(model.getPartner_id());
        if(model.getPutaway_strategy_idDirtyFlag())
            domain.setPutaway_strategy_id(model.getPutaway_strategy_id());
        if(model.getRemoval_strategy_idDirtyFlag())
            domain.setRemoval_strategy_id(model.getRemoval_strategy_id());
        return domain ;
    }


}
